package baidumodel.entity;

import java.util.Arrays;
import java.util.Objects;

/**
 * 错误码枚举类
 *
 * @Author wuchenxi
 * @Date 2023-08-12 18:30:10
 */
public enum CodeEnum {
    UnknownError(1, "Unknown error", "服务器内部错误，请再次请求， 如果持续出现此类错误，请在百度云控制台内提交工单反馈"),
    ServiceTemporarilyUnavailable(2, "Service temporarily unavailable", "Service temporarily unavailable"),
    UnsupportedOpenapiMethod(3, "Unsupported openapi method", "调用的API不存在，请检查后重新尝试"),
    OpenApiRequestLimitReached(4, "Open api request limit reached", "集群超限额"),
    NoPermissionToAccessData(6, "No permission to access data", "无权限访问该用户数据"),
    GetServiceTokenFailed(13, "Get service token failed", "获取token失败"),
    IAMCertificationFailed(14, "IAM Certification failed", "IAM鉴权失败"),
    AppNotExistsOrCreateFailed(15, "app not exists or create failed", "应用不存在或者创建失败"),
    OpenApiDailyRequestLimitReached(17, "Open api daily request limit reached", "每天请求量超限额，已上线计费的接口，请直接在控制台开通计费，调用量不受限制，按调用量阶梯计费；未上线计费的接口，请在百度云控制台内提交工单反馈"),
    OpenApiQpsRequestLimitReached(18, "Open api qps request limit reached", "QPS超限额，已上线计费的接口，请直接在控制台开通计费，调用量不受限制，按调用量阶梯计费；未上线计费的接口，请在百度云控制台内提交工单反馈"),
    OpenApiTotalRequestLimitReached(19, "Open api total request limit reached", "请求总量超限额，已上线计费的接口，请直接在控制台开通计费，调用量不受限制，按调用量阶梯计费；未上线计费的接口，请在百度云控制台内提交工单反馈"),
    InvalidParameter(100, "Invalid parameter", "无效的access_token参数，请检查后重新尝试"),
    AccessTokenInvalidOrNoLongerValid(110, "Access token invalid or no longer valid", "access_token无效"),
    AccessTokenExpired(111, "Access token expired", "access token过期"),
    InternalError(336000, "Internal error", "服务内部错误"),
    InvalidArgument(336001, "Invalid Argument", "入参格式有误，比如缺少必要参数"),
    InvalidJSON(336002, "Invalid JSON", "入参body不是标准的JSON格式"),
    APINameNotExist(336005, "API name not exist", "API name not exist"),
    InvalidHTTPMethod(336101, "Invalid HTTP Method", "非法的HTTP Method， 当前仅支持POST请求");

    private Integer code;
    private String errMsg;
    private String desc;

    /**
     * @param code   错误码
     * @param errMsg 异常信息
     * @param desc   描述
     */
    CodeEnum(int code, String errMsg, String desc) {
        this.code = code;
        this.errMsg = errMsg;
        this.desc = desc;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    /**
     * 获取错误码详细信息
     *
     * @param code 错误码
     * @return 错误码信息，注意处理 null
     */
    public static CodeEnum getByCode(Integer code) {
        if (code == null) {
            return null;
        }
        return Arrays.stream(CodeEnum.values()).filter(item -> Objects.equals(item.getCode(), code)).findFirst().orElse(null);
    }
}
